/* eslint-disable vue/no-unused-vars */
<template>
  <div class="container">
    <div class="seachAndState">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="时间粒度">
          <el-select
            v-model="formInline.time"
            placeholder="选择时间段"
            @click="app"
          >
            <el-option label="分钟" value="Min"></el-option>
            <el-option label="十分钟" value="Ten"></el-option>
            <el-option label="三十分钟" value="Thirty"></el-option>
            <el-option label="一小时" value="Hour"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="formInline.time !== 'Min'" label="数据">
          <el-select v-model="formInline.data" placeholder="选择时间段">
            <el-option label="平均值" value="平均值"></el-option>
            <el-option label="最小值" value="最小值"></el-option>
            <el-option label="最大值" value="最大值"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="formInline.timeLine"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd hh:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="getAllData('change')" >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="stateClass">
        状态:
        <div :class="judgeState ? 'greenLight' : 'redLight'"></div>
        <div style="font-size: 0.9rem; margin-top: 3px">
          {{ this.deviceStatus }}
        </div>
      </div>
    </div>
    <!-- 数据表格 -->
    <el-table
      :header-cell-style="getRowClass"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        fixed
        align="center"
        label="工艺名称"
        :width="tableData[0] ? 90 : ''"
      >
        坩埚
      </el-table-column>
      <el-table-column
        fixed
        align="center"
        label="时间"
        :width="tableData[0] ? 100 : ''"
      >
        <template slot-scope="{ row }">
          {{ row.createTime }}
        </template>
      </el-table-column>
      <!-- <el-table-column v-if="!tableData[0]" align="center" label="数据">
      </el-table-column> -->
      <el-table-column
        align="center"
        v-for="(item, index) in tableData[0] ? tableData[0].data : []"
        :key="index"
        :width="120"
      >
        <template slot="header">
          <div class="tableHead">{{ item.key }}</div>
          <div class="tableHead">({{ item.unit }})</div>
        </template>

        <template slot-scope="{ row }">
          {{ row.data[index].value }}
        </template>
      </el-table-column>
    </el-table>
    <div
      class="block"
      style="display: flex; flex-direction: row-reverse; margin-top: 10px"
    >
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currPage"
        :page-sizes="[10, 20, 30, 40]"
        :currPage-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { crucibleDataGet } from "@/api/crucible.js"
export default {
  name: "steepContent",
  data() {
    return {
      pageSize: 10,
      currPage: 1,
      total: null,
      change:false,
      deviceTypeId: "61d986ebfca8f66ec93084ad",
      formInline: {
        data: "平均值",
        time: "Min",
        timeLine: null,
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
      },
      tableData: [{ data: "" }],
      deviceStatus: null,
    }
  },
  created() {},
  mounted() {
    this.getAllData()
  },
  computed: {
    judgeState() {
      if (this.deviceStatus === "正常") {
        return true
      } else {
        return false
      }
    },
  },
  methods: {
    app() {
      console.log(this.formInline.time)
    },
    async getAllData(ret) {
      if (this.formInline.timeLine === null) {
        this.formInline.timeLine = ["", ""]
      }
      if (this.formInline.time === "Min") {
        const params = {
          BoxId: "2da580adb26b4a12accd4aec80e04656",
          pageSize: this.pageSize,
          currPage: this.currPage,
          dataTime: this.formInline.time,
          startTime: this.formInline.timeLine[0],
          endTime: this.formInline.timeLine[1],
        }

        const { data: res } = await crucibleDataGet(params)
        console.log(params)
        console.log(res)
        if (res.data) {
          this.tableData = res.data.dataSet
          this.total = res.data.totalCount
          this.deviceStatus = res.data.deviceStatus
          if(ret==='change'){
            this.$message({
              customClass: "mess-info",
              message: res.message,
            })
          }
        } else {
          this.tableData = [],
          this.total = 0,
            this.$message({
              customClass: "mess-info",
              message: res.message,
            })
        }
      }else {
        this.tableData = [],
          this.total = 0,
            this.$message({
              customClass: "mess-info",
              message: "暂无数据",
            })
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getAllData()
    },
    handleCurrentChange(val) {
      this.currPage = val
      this.getAllData()
    },
  },
}
</script>

<style  scoped>
 .el-pager .active {
  background-color: $primary !important;
}
 .el-table .el-table__cell {
  padding: 4px 0;
}
.container {
  .seachAndState {
    display: flex;
    justify-content: space-between;
    .stateClass {
      display: flex;
      height: 100%;
      padding: 5px 10px 10px 20px;
      width: 200px;
      .greenLight {
        margin: 6px 3px 4px 15px;
        width: 0.7rem;
        height: 0.7rem;
        background-color: rgb(133, 231, 163);
        border-radius: 0.4rem;
        border: 1px solid rgba(41, 241, 101, 0.349);
      }
      .redLight {
        margin: 6px 3px 4px 15px;
        width: 0.7rem;
        height: 0.7rem;
        background-color: rgb(219, 99, 99);
        border-radius: 0.4rem;
        border: 1px solid rgba(241, 148, 41, 0.349);
      }
    }
  }
}
</style>
